package com.etrans.web.gps.business.report.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.report.service.RemindAnalyzesService;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.grid.FlexiGridUtil;

@Controller
@RequestMapping("/report/remindAnalyzes/")
public class RemindAnalyzesController extends BaseController{
	
	@Autowired
	private RemindAnalyzesService RemindAnalyzesService;
	
	

	
	@RequestMapping("loadRemindAnalyzes.html")
	public String loadRemindAnalyzes(){
		return "views/business/report/remindAnalyzes";
		
	}

	@ResponseBody
	@RequestMapping("loadChartData.html")
	public Map<String,Object> loadChartData(String remindType,String reminder,String startDate,String endDate){
		return RemindAnalyzesService.loadChartData(remindType,reminder,startDate,endDate);
		
	}
	
	@ResponseBody
	@RequestMapping("loadTableData.html")
	public PageVo loadTableData(String paramsGrid){
		PageVo vo = new PageVo();
		try {
			Map<String, Object> param = FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
			List<Document> list = RemindAnalyzesService.loadTableData(param);
			int count = RemindAnalyzesService.loadTableDataCount(param);
			vo.setPage((int) param.get("page"));
			vo.setRows(list);
			vo.setTotal(count);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vo;
	 
	}
	/**
	 *
	 *@description  导出查询结果
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "exportQueryData.html")
	public void exportQueryData( HttpServletRequest request, HttpServletResponse response, String startTime, String endTime,String remindType,
			String reminder,String title, String startPage, String endPage, String PageSize,String colModel,String imgSrc) {

		List<String> colModelList = JSON.parseObject(colModel, List.class);
		List<String> imgsrcList   = JSON.parseObject(imgSrc, List.class);
		int page = Integer.parseInt(startPage);
		int EndPage = Integer.parseInt(endPage);
		int pageSize =(EndPage - page)* Integer.parseInt(PageSize);
		int fromNumber = (page-1) * Integer.parseInt(PageSize);
		Map<String,Object> param =new HashMap<>();
		param.put("remindType", remindType);
		param.put("reminder", reminder);
		param.put("startDate", startTime);
		param.put("endDate", endTime);
		param.put("page",fromNumber);
		param.put("pageSize", pageSize);
		List<Document> list = RemindAnalyzesService.loadTableData(param);
		if (list != null && list.size() > 0) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			for (Document doc : list) {
				doc.replace("tips_type", doc.getInteger("tips_type")==0?"短信":"邮件");
				doc.replace("target_type", getTargetType(doc.getInteger("target_type")));
				doc.replace("tips_time", sdf.format(doc.getDate("tips_time")));
			}
		}
		
		int index = -1;
		List rowsList = new ArrayList<>();
		rowsList.add(list);
		exportExl(request, response,title, removeHeadName(colModelList,index,-1),rowsList,imgsrcList);
	}
	
	public String getTargetType(int target_type) {
		if(target_type == 0){
    		return "平台联系人";
    	}else if(target_type == 1){
    		return "业户联系人";
    	}else if(target_type == 2){
    		return "从业人员";
    	}else if(target_type == 3){
    		return "区域联系人";
    	}else{
    		return "车辆联系人";
    	}
	}
}
